﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using com.loto.Common.DBUtility;

namespace com.huigo.Framework.SqlserverDal
{
    public partial class InvoiceOrder
    {
        public static long AddInvoiceOrder(Model.InvoiceOrder invoiceOrder, SqlTransaction trans)
        {
            string sql = "insert into [HG_INVOICE_ORDER]({0}) values({1});SELECT @@IDENTITY;";
            List<string> f = new List<string>();
            List<string> v = new List<string>();
            List<SqlParameter> paraList = new List<SqlParameter>();
            SqlParameter para;

            if (!invoiceOrder.OrderId.Equals(long.MinValue))
            {
                f.Add(" ORDER_ID");
                v.Add(" @ORDER_ID");
                para = new SqlParameter("@ORDER_ID", SqlDbType.BigInt, 8);
                para.Value = invoiceOrder.OrderId;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderCustFid.Equals(long.MinValue))
            {
                f.Add(" ORDER_CUST_FID");
                v.Add(" @ORDER_CUST_FID");
                para = new SqlParameter("@ORDER_CUST_FID", SqlDbType.BigInt, 8);
                para.Value = invoiceOrder.OrderCustFid;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderNo.Equals(string.Empty))
            {
                f.Add(" ORDER_NO");
                v.Add(" @ORDER_NO");
                para = new SqlParameter("@ORDER_NO", SqlDbType.NVarChar, 20);
                para.Value = invoiceOrder.OrderNo;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderProductFid.Equals(long.MinValue))
            {
                f.Add(" ORDER_PRODUCT_FID");
                v.Add(" @ORDER_PRODUCT_FID");
                para = new SqlParameter("@ORDER_PRODUCT_FID", SqlDbType.BigInt, 8);
                para.Value = invoiceOrder.OrderProductFid;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderProductName.Equals(string.Empty))
            {
                f.Add(" ORDER_PRODUCT_NAME");
                v.Add(" @ORDER_PRODUCT_NAME");
                para = new SqlParameter("@ORDER_PRODUCT_NAME", SqlDbType.NVarChar, 30);
                para.Value = invoiceOrder.OrderProductName;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_AMOUNT");
                v.Add(" @ORDER_AMOUNT");
                para = new SqlParameter("@ORDER_AMOUNT", SqlDbType.Money, 8);
                para.Value = invoiceOrder.OrderAmount;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderPayAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_PAY_AMOUNT");
                v.Add(" @ORDER_PAY_AMOUNT");
                para = new SqlParameter("@ORDER_PAY_AMOUNT", SqlDbType.Money, 8);
                para.Value = invoiceOrder.OrderPayAmount;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderDiscountAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_DISCOUNT_AMOUNT");
                v.Add(" @ORDER_DISCOUNT_AMOUNT");
                para = new SqlParameter("@ORDER_DISCOUNT_AMOUNT", SqlDbType.Money, 8);
                para.Value = invoiceOrder.OrderDiscountAmount;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderScoreAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_SCORE_AMOUNT");
                v.Add(" @ORDER_SCORE_AMOUNT");
                para = new SqlParameter("@ORDER_SCORE_AMOUNT", SqlDbType.Money, 8);
                para.Value = invoiceOrder.OrderScoreAmount;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderCreatedOn.Equals(DateTime.MinValue))
            {
                f.Add(" ORDER_CREATED_ON");
                v.Add(" @ORDER_CREATED_ON");
                para = new SqlParameter("@ORDER_CREATED_ON", SqlDbType.DateTime, 8);
                para.Value = invoiceOrder.OrderCreatedOn;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderPaidOn.Equals(DateTime.MinValue))
            {
                f.Add(" ORDER_PAID_ON");
                v.Add(" @ORDER_PAID_ON");
                para = new SqlParameter("@ORDER_PAID_ON", SqlDbType.DateTime, 8);
                para.Value = invoiceOrder.OrderPaidOn;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderLastUpdatedOn.Equals(DateTime.MinValue))
            {
                f.Add(" ORDER_LAST_UPDATED_ON");
                v.Add(" @ORDER_LAST_UPDATED_ON");
                para = new SqlParameter("@ORDER_LAST_UPDATED_ON", SqlDbType.DateTime, 8);
                para.Value = invoiceOrder.OrderLastUpdatedOn;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderSource.Equals(byte.MaxValue))
            {
                f.Add(" ORDER_SOURCE");
                v.Add(" @ORDER_SOURCE");
                para = new SqlParameter("@ORDER_SOURCE", SqlDbType.TinyInt, 1);
                para.Value = invoiceOrder.OrderSource;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderFrom.Equals(byte.MaxValue))
            {
                f.Add(" ORDER_FROM");
                v.Add(" @ORDER_FROM");
                para = new SqlParameter("@ORDER_FROM", SqlDbType.TinyInt, 1);
                para.Value = invoiceOrder.OrderFrom;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderStatusFid.Equals(int.MinValue))
            {
                f.Add(" ORDER_STATUS_FID");
                v.Add(" @ORDER_STATUS_FID");
                para = new SqlParameter("@ORDER_STATUS_FID", SqlDbType.Int, 4);
                para.Value = invoiceOrder.OrderStatusFid;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderPaperCount.Equals(int.MinValue))
            {
                f.Add(" ORDER_PAPER_COUNT");
                v.Add(" @ORDER_PAPER_COUNT");
                para = new SqlParameter("@ORDER_PAPER_COUNT", SqlDbType.Int, 4);
                para.Value = invoiceOrder.OrderPaperCount;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderDeliverType.Equals(byte.MaxValue))
            {
                f.Add(" ORDER_DELIVER_TYPE");
                v.Add(" @ORDER_DELIVER_TYPE");
                para = new SqlParameter("@ORDER_DELIVER_TYPE", SqlDbType.TinyInt, 1);
                para.Value = invoiceOrder.OrderDeliverType;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderProvince.Equals(string.Empty))
            {
                f.Add(" ORDER_PROVINCE");
                v.Add(" @ORDER_PROVINCE");
                para = new SqlParameter("@ORDER_PROVINCE", SqlDbType.NVarChar, 10);
                para.Value = invoiceOrder.OrderProvince;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderCity.Equals(string.Empty))
            {
                f.Add(" ORDER_CITY");
                v.Add(" @ORDER_CITY");
                para = new SqlParameter("@ORDER_CITY", SqlDbType.NVarChar, 20);
                para.Value = invoiceOrder.OrderCity;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderAddress.Equals(string.Empty))
            {
                f.Add(" ORDER_ADDRESS");
                v.Add(" @ORDER_ADDRESS");
                para = new SqlParameter("@ORDER_ADDRESS", SqlDbType.NVarChar, 100);
                para.Value = invoiceOrder.OrderAddress;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderContact.Equals(string.Empty))
            {
                f.Add(" ORDER_CONTACT");
                v.Add(" @ORDER_CONTACT");
                para = new SqlParameter("@ORDER_CONTACT", SqlDbType.NVarChar, 30);
                para.Value = invoiceOrder.OrderContact;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderTel.Equals(string.Empty))
            {
                f.Add(" ORDER_TEL");
                v.Add(" @ORDER_TEL");
                para = new SqlParameter("@ORDER_TEL", SqlDbType.NVarChar, 30);
                para.Value = invoiceOrder.OrderTel;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderRealPaperCount.Equals(int.MinValue))
            {
                f.Add(" ORDER_REAL_PAPER_COUNT");
                v.Add(" @ORDER_REAL_PAPER_COUNT");
                para = new SqlParameter("@ORDER_REAL_PAPER_COUNT", SqlDbType.Int, 4);
                para.Value = invoiceOrder.OrderRealPaperCount;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderFedexNo.Equals(string.Empty))
            {
                f.Add(" ORDER_FEDEX_NO");
                v.Add(" @ORDER_FEDEX_NO");
                para = new SqlParameter("@ORDER_FEDEX_NO", SqlDbType.NVarChar, 30);
                para.Value = invoiceOrder.OrderFedexNo;
                paraList.Add(para);
            }
            if (!invoiceOrder.OrderFedex.Equals(string.Empty))
            {
                f.Add(" ORDER_FEDEX");
                v.Add(" @ORDER_FEDEX");
                para = new SqlParameter("@ORDER_FEDEX", SqlDbType.NVarChar, 10);
                para.Value = invoiceOrder.OrderFedex;
                paraList.Add(para);
            }

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = string.Format(sql, string.Join(",", f.ToArray()), string.Join(",", v.ToArray()));
            cmd.Parameters.AddRange(paraList.ToArray());
            try
            {
                return Convert.ToInt64(SqlHelper.ExecuteScalar(SqlHelper.MainSQLConnString, cmd, trans));
            }
            catch (Exception exce)
            {
                throw exce;
            }
        }
    }
}
